package com.Dao.Imp;
import com.Dao.InsertUserDao;
import com.Utils.C3P0Utils;
import com.pojo.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
public class InsertUserDaoImp implements InsertUserDao {
    @Override
    public int Insert(String username, String password) throws SQLException {
        int flag=0;
        QueryRunner queryRunner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="select id,username,password,status from userlist where username=?";
        List<User>list=queryRunner.query(sql,new BeanListHandler<User>(User.class),username);
        long timestamp = System.currentTimeMillis();
        String nickname="豆友"+username;
        if(!list.isEmpty()){//用户存在
            flag=1;
        }else{
            sql="insert into userlist(username,password,nickname,registTime)values (?,?,?,?)";
            queryRunner.update(sql,username,password,nickname,timestamp);
            flag=2;
        }
        return flag;
    }
}
